package client;

import java.net.*;
import java.util.Scanner;
import java.io.*;
import data.*;
import util.*;
import java.util.*;

/**
 * 
 * @author Hiuling Ng and Waiyip Cheng April 27th, 2013
 */

public class SimpleClient {

	/**
	 * @param args
	 */

	public static LinkedList<Note> noteList;
	private Socket s1;
	private InputStream s1In;
	private DataInputStream dis;

        public SimpleClient() {
            noteList = new LinkedList<Note>();
        }
        
	public LinkedList<Note> testClient() throws Exception {
		
		//read ip and port num
		System.out.println("Please input server's ip addr and port\n(for example: 127.0.0.1:80)");
		
		Scanner sc = new Scanner(System.in);
		String temp = sc.nextLine();
		sc.close();
		String ipInfo[] = temp.split(":");
		String ip = ipInfo[0];
		String port = ipInfo[1];

		//create socket
		s1 = new Socket(ip, Integer.parseInt(port));

		//io stream
		s1In = s1.getInputStream();
		dis = new DataInputStream(s1In);
		ObjectInputStream ois = new ObjectInputStream(s1In);

		// String st = new String(dis.readUTF());
		// System.out.println(st);

		//read note list from server
		noteList = (LinkedList<Note>) ois.readObject();

		//get all notes
		for (Note note : noteList) {
                    //if (note.name == ???)
			System.out.println(note.title);
			System.out.println(note.content);
			System.out.println(MyDate.getDate());
		
                }

		//close io stream and socket
		ois.close();
		dis.close();
		s1In.close();
		s1.close();
                return noteList;
	}
	
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub		
		SimpleClient client = new SimpleClient();
		
		client.testClient();		
	}

}
